<!--
 * @Description: ------------ fileDescription -----------
 * @Author: snows_l snows_l@163.com
 * @Date: 2024-08-08 11:01:12
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2025-01-20 10:11:28
 * @FilePath: \BLOG\src\views\article\index.vue
-->
<template>
  <div class="article-detail-warp">
    <PageTopCover
      :moduleTitle="state.currentModule.moduleTitle"
      :icon="state.currentModule.moduleIcon"
      :textIcon="state.currentModule.moduleTextIcon"
      :imgIcon="state.currentModule.moduleImgIcon"
      :coverImg="state.currentModule.muduleCover"
      :isMobile="isMobi"
      :mudulDesc="state.currentModule.moduleDesc"
      :isArticle="false"></PageTopCover>
    <div class="article-content-warp-out" :class="{ 'm-article-content-warp-out': isMobi }">
      <div class="other-content" v-if="!isMobi">
        <div class="article-content-warp" v-if="state.articleList.length > 0">
          <div
            class="article-item kbn-custom intersection-observer hover-shadow"
            :style="{ marginBottom: index === state.articleList.length - 1 && state.page.total <= state.page.size ? '20px' : '0px' }"
            :data-tip="item.title.length > 16 ? '你想要阅读的文章“' + item.title.substring(0, 16) + '...”这篇文章吗？' : '你想要阅读的文章“' + item.title + '”这篇文章吗？'"
            v-for="(item, index) in state.articleList"
            :key="index">
            <div class="img-left item-warp" v-if="index % 2 === 0">
              <div class="cover-img-warp pointer" @click="handleArticle(item)">
                <LImg class="cover-img" :src="item.cover" :size="isMobi ? '120px' : '160px'" :isUnPreview="true" />
              </div>
              <div class="item-content">
                <div class="create-time">
                  <div class="time-warp">
                    <IconCalendar class="iconfont"></IconCalendar>
                    <span>发布于：</span>
                    <span class="time">{{ item.createTime }}</span>
                  </div>
                </div>
                <div class="article-title pointer" @click="handleArticle(item)">
                  {{ item.title }}
                </div>
                <div class="auth-info-warp">
                  <div>
                    <img :src="state.avatar" alt="" />
                    <span>snows_l</span>
                  </div>
                  <div>
                    <IconHot class="iconfont"></IconHot>
                    <span>{{ item.readCount || 0 }}</span>
                  </div>
                  <div>
                    <IconLike :size="18" class="iconfont" />
                    <span>{{ item.shareCount || 0 }}</span>
                  </div>
                  <div>
                    <IconComment :size="19" class="iconfont"></IconComment>
                    <span>{{ item.commentCount || 0 }}</span>
                  </div>
                  <div class="pointer" data-tip="点击 可直接阅览" v-if="item.isPreview === 1" @click="handleView(item)">
                    <IconPreview class="iconfont" :size="18"></IconPreview>
                    <span class="" style="font-size: 12px">预览</span>
                  </div>
                </div>
                <div class="article-des text">
                  {{ item.subTitle }}
                </div>
                <div class="label-warp">
                  <div class="type pointer" @click="handleType(item.type)">
                    <span class="icon-warp">
                      <IconType class="iconfont"></IconType>
                    </span>
                    <span class="lable" :style="{ 'background-color': 'var(--theme-light-color-2)' }">
                      {{ state.articleTypeList.find(v => v.value == item.type)?.label || '未知类型' }}
                    </span>
                  </div>
                  <div class="label-item pointer" @click="handleCategory(label)" v-for="(label, index) in item.labels" :key="index">
                    <span class="icon-warp">
                      <IconLabel class="iconfont" :size="'14px'"></IconLabel>
                    </span>
                    <span class="lable" :style="{ 'background-color': colorMap[index] }">{{ label }}</span>
                  </div>
                </div>
              </div>
            </div>
            <div class="img-right item-warp" v-else>
              <div class="item-content">
                <div class="create-time">
                  <div class="time-warp">
                    <IconCalendar class="iconfont"></IconCalendar>
                    <span>发布于：</span>
                    <span class="time">{{ item.createTime }}</span>
                  </div>
                </div>
                <div class="article-title pointer" @click="handleArticle(item)">
                  {{ item.title }}
                </div>
                <div class="auth-info-warp">
                  <div>
                    <img :src="state.avatar" alt="" />
                    <span>snows_l</span>
                  </div>
                  <div>
                    <IconHot class="iconfont"></IconHot>
                    <span>{{ item.readCount || 0 }}</span>
                  </div>
                  <div>
                    <IconLike :size="18" class="iconfont" />
                    <span>{{ item.shareCount || 0 }}</span>
                  </div>
                  <div>
                    <IconComment :size="19" class="iconfont"></IconComment>
                    <span>{{ item.commentCount || 0 }}</span>
                  </div>
                  <div class="pointer" data-tip="点击 可直接阅览" v-if="item.isPreview === 1" @click="handleView(item)">
                    <IconPreview class="iconfont" :size="18"></IconPreview>
                    <span class="" style="font-size: 12px">预览</span>
                  </div>
                </div>
                <div class="article-des text">
                  {{ item.subTitle }}
                </div>
                <div class="label-warp">
                  <div class="type pointer" @click="handleType(item.type)">
                    <span class="icon-warp">
                      <IconType class="iconfont"></IconType>
                    </span>
                    <span class="lable" :style="{ 'background-color': 'var(--theme-light-color-2)' }">
                      {{ state.articleTypeList.find(v => v.value == item.type)?.label || '未知类型' }}
                    </span>
                  </div>
                  <div class="label-item pointer" @click="handleCategory(label)" v-for="(label, index) in item.labels" :key="index">
                    <span class="icon-warp">
                      <IconLabel class="iconfont" :size="'14px'"></IconLabel>
                    </span>
                    <span class="lable" :style="{ 'background-color': colorMap[index] }">{{ label }}</span>
                  </div>
                </div>
              </div>
              <div class="cover-img-warp pointer" @click="handleArticle(item)">
                <LImg class="cover-img" :src="item.cover" :size="isMobi ? '120px' : '160px'" :isUnPreview="true" />
              </div>
            </div>
          </div>
        </div>

        <div class="no-article" v-else>
          <Empty :text="'暂无文章，期待您的分享~'" :loadingText="'文章正在拼命加载中...'" :loading="state.loading" />
        </div>
      </div>
      <div class="other-content m-other-content" v-else>
        <div class="article-content-warp" v-if="state.articleList.length > 0">
          <div
            class="article-item kbn-custom intersection-observer hover-shadow"
            :data-tip="item.title.length > 16 ? '你想要阅读的文章“' + item.title.substring(0, 16) + '...”这篇文章吗？' : '你想要阅读的文章“' + item.title + '”这篇文章吗？'"
            v-for="(item, index) in state.articleList"
            :key="index">
            <div class="img-left item-warp">
              <div class="cover-img-warp pointer" @click="handleArticle(item)">
                <LImg class="cover-img" :src="item.cover" :size="isMobi ? '120px' : '160px'" :isUnPreview="true" />
              </div>
              <div class="item-content">
                <div class="create-time">
                  <div class="time-warp">
                    <IconCalendar class="iconfont"></IconCalendar>
                    <span>发布于：</span>
                    <span class="time">{{ item.createTime }}</span>
                  </div>
                </div>
                <div class="article-title pointer" @click="handleArticle(item)">
                  {{ item.title }}
                </div>
                <div class="auth-info-warp">
                  <div>
                    <img :src="state.avatar" vertical-align="middle" alt="" />
                    <span>snows_l</span>
                  </div>
                  <div>
                    <IconHot class="iconfont"></IconHot>
                    <span>{{ item.readCount || 0 }}</span>
                  </div>
                  <div>
                    <IconLike :size="18" class="iconfont" />
                    <span>{{ item.shareCount || 0 }}</span>
                  </div>
                  <div>
                    <IconComment :size="19" class="iconfont"></IconComment>
                    <span>{{ item.commentCount || 0 }}</span>
                  </div>
                  <div class="pointer" data-tip="点击 可直接阅览" v-if="item.isPreview === 1" @click="handleView(item)">
                    <IconPreview class="iconfont" :size="18"></IconPreview>
                    <span class="" style="font-size: 12px">预览</span>
                  </div>
                </div>
                <div class="article-des text">
                  {{ item.subTitle }}
                </div>
                <div class="label-warp">
                  <div class="type pointer" @click="handleType(item.type)">
                    <span class="icon-warp">
                      <IconType class="iconfont"></IconType>
                    </span>
                    <span class="lable" :style="{ 'background-color': 'var(--theme-light-color-2)' }">
                      {{ state.articleTypeList.find(v => v.value == item.type)?.label || '未知类型' }}
                    </span>
                  </div>
                  <div class="label-item pointer" @click="handleCategory(label)" v-for="(label, index) in item.labels" :key="index">
                    <span class="icon-warp">
                      <IconLabel class="iconfont" :size="'14px'"></IconLabel>
                    </span>
                    <span class="lable" :style="{ 'background-color': colorMap[index] }">{{ label }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="m-no-article" v-else>
          <Empty :text="'暂无文章，期待您的分享~'" :loadingText="'文章正在拼命加载中...'" :loading="state.loading" />
        </div>
      </div>
      <LoadingMore
        v-if="store.projectConfig.pageType == 'more'"
        :loading="state.loading"
        :hasMore="state.isMore"
        :length="state.articleList.length"
        @loadMore="handleLoadMore"></LoadingMore>
      <CustomPage v-else :page="state.page" @change="handlePageChange" :loading="state.loading" :scorllTop="'article-content-warp-out'"></CustomPage>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getArticleList } from '@/api/article';
import { getDict } from '@/api/common';
import { useAppStore } from '@/store/app';
import { getOnlineImgUrl, getQQAvatar } from '@/utils/common';
import { getRandomColor } from '@/utils/theme';
import moment from 'moment';
import { reactive, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';

import useIntersectionObserver from '@/hooks/useIntersectionObserver';
import useResize from '@/hooks/useResize';
const { isMobi } = useResize();
useIntersectionObserver();

const store = useAppStore();
const router = useRouter();
const route = useRoute();

type ArticleType = '1' | '2' | '3' | '4';

const typeMap = {
  '1': {
    moduleTitle: '前端',
    moduleTextIcon: '📕',
    muduleCover: getOnlineImgUrl('/imgs/bg/117.png'),
    moduleDesc: ''
  },
  '2': {
    moduleTitle: '后端',
    moduleImgIcon: 'Nodejs.png',
    muduleCover: getOnlineImgUrl('/imgs/bg/185.png'),
    moduleDesc: ''
  },
  '3': {
    moduleTitle: '其他',
    moduleTextIcon: '📘',
    muduleCover: getOnlineImgUrl('/imgs/bg/126.png'),
    moduleDesc: ''
  },
  '4': {
    moduleTitle: route.query.category,
    moduleTextIcon: '📗',
    muduleCover: getOnlineImgUrl('/imgs/bg/31.png'),
    moduleDesc: ''
  }
};

const colorMap = {
  0: getRandomColor(),
  1: getRandomColor(),
  2: getRandomColor(),
  3: getRandomColor(),
  4: getRandomColor(),
  5: getRandomColor(),
  6: getRandomColor(),
  7: getRandomColor(),
  8: getRandomColor(),
  9: getRandomColor()
};

type TypeMap = typeof typeMap;

const state = reactive({
  avatar: getQQAvatar(),
  loading: false,
  isMore: true,
  page: {
    page: 1,
    size: 10,
    total: 0
  },
  articleList: [],
  articleTypeList: [],
  currentModule: route.query.type ? typeMap[route.query.type as ArticleType] : typeMap['1']
});

interface ListParams {
  page: number;
  size: number;
  [key: string]: any;
}

// 获取文章列表
const getArticleListFn = scorllTopCallback => {
  state.loading = true;
  const params: ListParams = { ...state.page };
  if (route.query.type == '4') {
    params.label = route.query.category;
  } else {
    params.type = route.query.type;
  }
  getArticleList(params)
    .then(res => {
      if (res.code === 200) {
        res.data.forEach(item => {
          item.createTime = moment(item.createTime).format('YYYY-MM-DD HH:mm:ss');
          item.cover = import.meta.env.VITE_CURRENT_ENV == 'dev' ? import.meta.env.VITE_DEV_BASE_SERVER + item.cover : store.projectConfig.bsServerUrl + item.cover;
          item.labels = item.labels ? item.labels.split(',') : [];
        });
        if (store.projectConfig.pageType == 'more') {
          state.articleList = [...state.articleList, ...res.data];
        } else {
          state.articleList = res.data;
        }
        state.page.total = res.total;
        if (state.articleList.length >= res.total) {
          state.isMore = false; // 已经没有更多数据了
        }
        scorllTopCallback && scorllTopCallback();
        state.loading = false;
      }
    })
    .catch(() => {
      state.isMore = false;
    })
    .finally(() => {
      state.loading = false;
    });
};
getArticleListFn();

// 获取文章类型
const getArticleTypeList = () => {
  getDict({ dictType: 'article_type' }).then(res => {
    state.articleTypeList = res.data;
    store.SET_ARTICLE_DICT(res.data);
  });
};

store.articleDict.length > 0 ? (state.articleTypeList = store.articleDict) : getArticleTypeList();

// 点击文章详情
const handleArticle = row => {
  router.push({
    path: '/article/detail',
    query: {
      id: row.id
    }
  });
};

const handleView = (row: any) => {
  let urlPrefix = store.projectConfig.blogUrl;
  window.open(urlPrefix + '/preview?id=' + row.id, '_blank');
};

// 加在更多
const handleLoadMore = () => {
  state.loading = true;
  state.page.page++;
  getArticleListFn();
};

// 分页改变
const handlePageChange = (page: number, size: number, scorllTopCallback: Function) => {
  state.page.page = page;
  state.page.size = size;
  getArticleListFn(scorllTopCallback);
};

// 点击文章类型
const handleType = (type: number) => {
  router.push({
    path: '/article/category',
    query: {
      type: type
    }
  });
};

// 点击文章分类
const handleCategory = (label: string) => {
  router.push({
    path: '/article/category',
    query: {
      type: 4,
      category: label
    }
  });
};

// 重置
const reset = () => {
  state.page.page = 1;
  state.page.total = 0;
  state.articleList = [];
  state.isMore = true;
  getArticleListFn();
};

watch(
  () => route.query.type,
  n => {
    if (n != '4') {
      state.currentModule = typeMap[n as ArticleType];
      reset();
    }
  }
);

watch(
  () => route.query.category,
  n => {
    if (route.query.type == '4') {
      state.currentModule = typeMap[route.query.type];
      state.currentModule.moduleTitle = n;
      reset();
    }
  }
);
</script>

<style lang="scss" scoped>
.article-detail-warp {
  width: 100%;
  height: 100%;
  min-height: 100vh;
  .article-content-warp-out {
    width: 100%;
    height: 100%;
    min-height: calc(100vh - 400px);
    background-color: var(--bg-image-warp-color);
    .article-content-warp {
      padding: 5px 0;
      max-width: var(--content-max-width);
      margin: 0 auto;
      .article-item {
        margin: 24px 0;
        border-radius: var(--border-radius-2);
        width: 100%;
        height: 100%;
        overflow: hidden;
        box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
        &:last-child {
          margin-bottom: 0;
        }
        &:hover {
          .cover-img {
            transform: scale(1.2);
          }
        }
        .item-warp {
          display: flex;
          width: 100%;
        }
        .cover-img-warp {
          width: 450px;
          height: 280px;
          overflow: hidden;
          .cover-img {
            width: 100%;
            height: 100%;
            transition: all 0.8s ease-in-out;
          }
        }
        .item-content {
          flex: 1;
          height: 280px;
          padding: 33px 30px;
          background-color: var(--bg-content-color);
          position: relative;
          .create-time {
            align-items: center;
            height: 24px;
            font-size: 12px;
            padding: 2px 0px;
            border-radius: 5px;
            color: var(--theme-color);
            .time-warp {
              height: 100%;
              display: flex;
              align-items: center;
              .iconfont {
                font-size: 12px;
                margin-right: 10px;
              }
            }
            .type {
              height: 22px;
              border-radius: 5px;
              padding: 2px 4px;
              font-size: 12px;
              position: absolute;
              top: 48px;
              right: 30px;
            }
          }
          .article-title {
            margin-top: 10px;
            height: 30px;
            font-size: 20px;
            line-height: 30px;
            cursor: url('@/assets/images/cursor/text.png'), auto;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 1;
            overflow: hidden;
            text-overflow: ellipsis;
            text-align: left;
            color: var(--text-color);
          }
          .article-title:hover {
            color: var(--theme-color);
          }
          .auth-info-warp {
            height: 50px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            div {
              display: flex;
              align-items: center;
              .iconfont {
                margin-right: 10px;
                font-size: 20px;
                color: var(--theme-color);
              }
              .icon-yanjing {
                font-size: 24px;
              }
              span {
                font-size: 12px;
                color: var(--text-color);
              }
            }
            img {
              width: 30px;
              height: 30px;
              border-radius: 50%;
              margin-right: 10px;
            }
          }
          .article-des {
            height: 64px;
            line-height: 21px;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 3;
            overflow: hidden;
            text-overflow: ellipsis;
            color: var(--text-color-2);
          }
          .label-warp {
            display: flex;
            align-items: center;
            justify-content: flex-start;
            margin-top: 10px;
            .type {
              font-size: 12px;
              border-radius: 5px;
              margin-right: 15px;
              height: 20px;
              display: flex;
              align-items: center;
              overflow: hidden;
              margin-bottom: 2px;
              .icon-warp {
                padding: 1px 6px 1px 8px;
              }
            }
            .label-item {
              margin-right: 10px;
              font-size: 12px;
              display: flex;
              align-items: center;
              border-radius: var(--border-radius-1);
              overflow: hidden;
              height: 20px;
              margin-bottom: 2px;
            }
            .icon-warp,
            .lable {
              height: 100%;
              display: inline-block;
            }
            .icon-warp {
              padding: 3px 6px 3px 8px;
              background-color: #b3b0b0;
            }
            .lable {
              color: #fff;
              padding: 4px 10px 4px 6px !important;
              font-size: 12px;
              text-wrap: nowrap;
            }
          }
        }
      }
      // .img-left {
      //   .time-warp {
      //     width: 100%;
      //     display: flex !important;
      //     justify-content: flex-end;
      //   }
      //   .article-title {
      //     text-align: right !important;
      //   }
      //   .article-des {
      //     text-align: right !important;
      //   }
      //   .auth-info-warp {
      //     flex-direction: row-reverse;
      //   }
      //   .label-warp {
      //     flex-direction: row-reverse;
      //     justify-content: flex-end;
      //   }
      // }
    }
    .no-article {
      margin: 0 auto;
      max-width: var(--content-max-width);
    }
  }
  .m-article-content-warp-out {
    .other-content {
      max-width: var(--content-max-width-m);
      min-height: calc(100vh - 300px);
      margin: 0 auto;
      .item-warp {
        display: flex;
        flex-direction: column;
        height: auto !important;
        .cover-img-warp {
          width: 100% !important;
          height: 180px !important;
        }

        .item-content {
          height: 180px;
          padding: 10px 20px !important;
          .article-title {
            margin-top: 5px !important;
            height: 30px !important;
            line-height: 30px !important;
            font-size: 16px !important;
          }
          .type {
            top: 10px !important;
            right: 20px !important;
          }
        }
        .auth-info-warp {
          height: 30px !important;
          img {
            width: 20px !important;
            height: 20px !important;
          }
        }
        .article-des {
          height: 30px !important;
          line-height: 15px !important;
          font-size: 12px !important;
          -webkit-line-clamp: 2 !important;
        }
      }
    }
  }
  .bottom-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    .btn-more {
      margin-bottom: 20px;
      padding: 10px 20px;
      border-radius: 36px;
      background-color: var(--theme-light-color-9);
      color: var(--theme-color);
      border: 1px solid var(--theme-light-color-5);
    }
    .no-more {
      color: var(--theme-light-color-3);
      margin-bottom: 20px;
    }
  }

  .no-article {
    height: calc(100vh - 400px);
  }
  .m-no-article {
    height: calc(100vh - 300px);
  }
}
</style>
